package cn.ibizlab.odoo.core.odoo_crm.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Icrm_activity_report;
import cn.ibizlab.odoo.core.odoo_crm.domain.Crm_activity_report;
import cn.ibizlab.odoo.core.odoo_crm.filter.Crm_activity_reportSearchContext;
import cn.ibizlab.odoo.core.odoo_crm.service.ICrm_activity_reportService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[CRM活动分析] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Crm_activity_reportServiceImpl implements ICrm_activity_reportService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Icrm_activity_reportClientService crm_activity_reportClientService;

    @Override
    public void updateBatch(List<Crm_activity_report> crm_activity_reports, int batchSize) {

        //crm_activity_reportClientService.updateBatch(crm_activity_reports,batchSize) ;

    }

    @Override
    public boolean create(Crm_activity_report crm_activity_report) {
        boolean bOk = false ;
        Icrm_activity_report clientModel = this.convert2Model(crm_activity_report,null);
        crm_activity_reportClientService.create(clientModel) ;
        this.convert2Domain(clientModel,crm_activity_report);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Crm_activity_report crm_activity_report) {
        boolean bOk = false ;
        Icrm_activity_report clientModel = this.convert2Model(crm_activity_report,null);
        crm_activity_reportClientService.update(clientModel) ;
        this.convert2Domain(clientModel,crm_activity_report) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //crm_activity_reportClientService.removeBatch(crm_activity_reports) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Icrm_activity_report clientModel = crm_activity_reportClientService.createModel();
        clientModel.setId(id);
        crm_activity_reportClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Crm_activity_report get(Integer id) {
        Icrm_activity_report clientModel = crm_activity_reportClientService.createModel();
        clientModel.setId(id);
        crm_activity_reportClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Crm_activity_report get(Crm_activity_report crm_activity_report) {
        Crm_activity_report retcrm_activity_report = this.get(crm_activity_report.getId()) ;
    	BeanCopier copier=BeanCopier.create(retcrm_activity_report.getClass(), crm_activity_report.getClass(), false);
        copier.copy(retcrm_activity_report, crm_activity_report, null);
        return crm_activity_report ;
    }

    @Override
    public void createBatch(List<Crm_activity_report> crm_activity_reports, int batchSize) {

                        //crm_activity_reportClientService.createBatch(crm_activity_reports,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Crm_activity_report> searchDefault(Crm_activity_reportSearchContext context) {
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据线索/商机查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_lead(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据CRM 阶段查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_stage(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据销售渠道查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_team(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据活动类型查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_mail_activity_type(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据消息子类型查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_subtype(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_company(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据国家/地区查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_country(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_author(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_partner(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Crm_activity_report> selectByOdoo_user(Integer id){
        Crm_activity_reportSearchContext context = new Crm_activity_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_activity_report> list = new ArrayList<Crm_activity_report>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_activity_report> clientModelList = crm_activity_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_activity_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_activity_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Icrm_activity_report convert2Model(Crm_activity_report domain , Icrm_activity_report model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = crm_activity_reportClientService.createModel() ;
        }

         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getProbabilityDirtyFlag())
            model.setProbability(domain.getProbability());
         if(domain.getLead_typeDirtyFlag())
            model.setLead_type(domain.getLead_type());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getDateDirtyFlag())
            model.setDate(domain.getDate());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getSubjectDirtyFlag())
            model.setSubject(domain.getSubject());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getTeam_id_textDirtyFlag())
            model.setTeam_id_text(domain.getTeam_id_text());
         if(domain.getMail_activity_type_id_textDirtyFlag())
            model.setMail_activity_type_id_text(domain.getMail_activity_type_id_text());
         if(domain.getLead_id_textDirtyFlag())
            model.setLead_id_text(domain.getLead_id_text());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getSubtype_id_textDirtyFlag())
            model.setSubtype_id_text(domain.getSubtype_id_text());
         if(domain.getCountry_id_textDirtyFlag())
            model.setCountry_id_text(domain.getCountry_id_text());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getStage_id_textDirtyFlag())
            model.setStage_id_text(domain.getStage_id_text());
         if(domain.getAuthor_id_textDirtyFlag())
            model.setAuthor_id_text(domain.getAuthor_id_text());
         if(domain.getAuthor_idDirtyFlag())
            model.setAuthor_id(domain.getAuthor_id());
         if(domain.getCountry_idDirtyFlag())
            model.setCountry_id(domain.getCountry_id());
         if(domain.getStage_idDirtyFlag())
            model.setStage_id(domain.getStage_id());
         if(domain.getSubtype_idDirtyFlag())
            model.setSubtype_id(domain.getSubtype_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getLead_idDirtyFlag())
            model.setLead_id(domain.getLead_id());
         if(domain.getMail_activity_type_idDirtyFlag())
            model.setMail_activity_type_id(domain.getMail_activity_type_id());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getTeam_idDirtyFlag())
            model.setTeam_id(domain.getTeam_id());
        return model ;
    }

    public Crm_activity_report convert2Domain( Icrm_activity_report model ,Crm_activity_report domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Crm_activity_report();
        }

        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getProbabilityDirtyFlag())
            domain.setProbability(model.getProbability());
        if(model.getLead_typeDirtyFlag())
            domain.setLead_type(model.getLead_type());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getDateDirtyFlag())
            domain.setDate(model.getDate());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getSubjectDirtyFlag())
            domain.setSubject(model.getSubject());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getTeam_id_textDirtyFlag())
            domain.setTeam_id_text(model.getTeam_id_text());
        if(model.getMail_activity_type_id_textDirtyFlag())
            domain.setMail_activity_type_id_text(model.getMail_activity_type_id_text());
        if(model.getLead_id_textDirtyFlag())
            domain.setLead_id_text(model.getLead_id_text());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getSubtype_id_textDirtyFlag())
            domain.setSubtype_id_text(model.getSubtype_id_text());
        if(model.getCountry_id_textDirtyFlag())
            domain.setCountry_id_text(model.getCountry_id_text());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getStage_id_textDirtyFlag())
            domain.setStage_id_text(model.getStage_id_text());
        if(model.getAuthor_id_textDirtyFlag())
            domain.setAuthor_id_text(model.getAuthor_id_text());
        if(model.getAuthor_idDirtyFlag())
            domain.setAuthor_id(model.getAuthor_id());
        if(model.getCountry_idDirtyFlag())
            domain.setCountry_id(model.getCountry_id());
        if(model.getStage_idDirtyFlag())
            domain.setStage_id(model.getStage_id());
        if(model.getSubtype_idDirtyFlag())
            domain.setSubtype_id(model.getSubtype_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getLead_idDirtyFlag())
            domain.setLead_id(model.getLead_id());
        if(model.getMail_activity_type_idDirtyFlag())
            domain.setMail_activity_type_id(model.getMail_activity_type_id());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getTeam_idDirtyFlag())
            domain.setTeam_id(model.getTeam_id());
        return domain ;
    }


}
